﻿using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace OurWedding1.IO
{
    public enum FontStyle { GrandHotelRegular, ItaliannoRegular }
    public class ConfirmationCard:IRendererable,IPdfPTableCreatable
    {
        public string ConfirmationText { get; set; }
        public string QrCodeUrl { get; set; }
        public string Key { get; set; }
        public string Signature { get; set; }
        public FontStyle FontStyle { get; set; }
        public void Render(Stream output)
        {
            var document = new Document(PageSize.A4, 25, 25, 30, 30);
            try
            {
                var writer = PdfWriter.GetInstance(document, output);
                document.Open();
                var table = CreateTable();
                document.Add(table);
                writer.Flush();
            }
            finally
            {

                document.Close();
            }
        }




        public static PdfPTable CreateConfirmationCardTable(ConfirmationCard input, Font textFont, Font fontsmall)
        {
            var qrcode = new BarcodeQRCode(input.QrCodeUrl, 100, 100, null);
            var qr = qrcode.GetImage();
            var table = new PdfPTable(2) {LockedWidth = true};
            var widths = new[] {110f, 150f};
            table.SetTotalWidth(widths);
            var cell = new PdfPCell(qr) {Padding = 2, Border = 0};

            var phrase = new Phrase(input.ConfirmationText, textFont);
            var cellText = new PdfPCell(phrase) {Border = 0, HorizontalAlignment = Element.ALIGN_JUSTIFIED};
            table.AddCell(cell);
            table.AddCell(cellText);
            var keyP = new Phrase(input.Key, fontsmall);
            var keyCell = new PdfPCell(keyP) {HorizontalAlignment = Element.ALIGN_CENTER,VerticalAlignment = Element.ALIGN_TOP, Border = 0};
            table.AddCell(keyCell);
            var signP = new Phrase(input.Signature, textFont);
            var signCell = new PdfPCell(signP) {HorizontalAlignment = Element.ALIGN_RIGHT, Border = 0};
            table.AddCell(signCell);
            return table;
        }

        public PdfPTable CreateTable()
        {
            Font textFont;
            Font smallFont;

            switch (FontStyle)
            {
                case FontStyle.GrandHotelRegular:

                    var grandHotelFontBase = BaseFont.CreateFont("GrandHotel_Regular.ttf", BaseFont.CP1250, BaseFont.EMBEDDED, BaseFont.CACHED, FontResources.GrandHotel_Regular, null);
                    textFont = new Font(grandHotelFontBase, 18);
                    smallFont = new Font(Font.FontFamily.HELVETICA, 5f);
                    break;
                case FontStyle.ItaliannoRegular:
                    var italiannoFontBase = BaseFont.CreateFont("Italianno_Regular.ttf", BaseFont.CP1250, BaseFont.EMBEDDED, BaseFont.CACHED, FontResources.Italianno_Regular, null);
                    textFont = new Font(italiannoFontBase, 18);
                    smallFont = new Font(Font.FontFamily.HELVETICA, 5f);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return CreateConfirmationCardTable(this, textFont, smallFont);
        }
    }
}

    
